# @Time : 2020/6/28 11:10
# @Author : Fioman 
# @Phone : 13149920693

# 写一个装饰器,打印函数运行的时间,传入的参数,和调用的返回结果打印出来.还有函数的名称
import time


def clock(func):
    def clocked(*args):
        t_start = time.perf_counter()
        res = func(*args)
        elapsed = time.perf_counter() - t_start
        name = func.__name__
        arg_str = ", ".join(repr(arg) for arg in args)
        print("{} {}({}) -> {}".format(elapsed, name, arg_str, res))
        return res

    return clocked


@clock
def snooze(seconds):
    time.sleep(seconds)


@clock
def factorial(n):
    return 1 if n < 2 else n * factorial(n - 1)

"""
装饰器的特点:
动态的给一个对象添加额外的功能.
上面的装饰器有几个缺点:
1> 不支持关键字参数,而且遮盖了被装饰函数的__name__和__doc__属性.我们可以使用functools.wraps装饰器把相关的属性从func复制到clocked中.
"""


if __name__ == '__main__':
    print("*" * 40,"Calling snooze(.123)")
    snooze(.123)
    print("*"*40,"Calling factorial(6)")
    print("6! = ",factorial(6))





